package com.movieknight;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
import android.view.KeyEvent;

// AddFriendSearchBrowser displays a search bar and provides methods relevant to searching.

public class AddFriendSearchBrowser extends Activity {
	private static final String LOG_TAG = "FriendSearchBrowser";
	EditText searchText;
	List<Friend> friend;
	Context context;
    static final int PICK_FRIEND_REQUEST = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Request a progress bar to be shown in the title.
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.friend_search);
		setProgressBarIndeterminateVisibility(true);
		
		// Configure Initial Options
		context = getApplicationContext();
		Button searchFriendButton = (Button) findViewById(R.id.mainSearchButton);
		searchText = (EditText) findViewById(R.id.mainSearchText);
		searchText.setOnEditorActionListener(exampleListener);
		
		searchFriendButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				String searchString = searchText.getText().toString();
				if (searchString.length() < 1) {
					Toast.makeText(context, getString(R.string.mainEmptySearch), Toast.LENGTH_SHORT).show();
					Log.v(LOG_TAG, "NoSearchString!");
				}
				
				// Ask remote server if the user exists.
				
				if (true) {
					// Prepare data intent 
					Intent data = new Intent();
					data.putExtra("returnKey1", searchString);
					// Activity finished okay, return the data
					setResult(RESULT_OK, data);
					finish();
				}
				// If the remote server does not find any users.
				else {
					Toast.makeText(context, getString(R.string.mainNoResult),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		
	}
	
	TextView.OnEditorActionListener exampleListener = new TextView.OnEditorActionListener(){
		public boolean onEditorAction(TextView searchText, int actionId, KeyEvent event) {
			   if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) { 
					String searchString = searchText.getText().toString();
					if (searchString.length() < 1) {
						Toast.makeText(context, getString(R.string.mainEmptySearch), Toast.LENGTH_SHORT).show();
						Log.v(LOG_TAG, "NoSearchString!");
						return true;
					}
					
					// Ask remote server if the user exists.
					
					if (true) {
						// Prepare data intent 
						Intent data = new Intent();
						data.putExtra("returnKey1", searchString);
						// Activity finished okay, return the data
						setResult(RESULT_OK, data);
						finish();
					}
					// If the remote server does not find any users.
					else {
						Toast.makeText(context, getString(R.string.mainNoResult),
								Toast.LENGTH_SHORT).show();
					}
			   }
			   return true;
			}
	};
	
	
	// Callback handler to stop progress bar
	Handler stopProgressHandler = new Handler() {
		public void handleMessage(Message m) {
			setProgressBarIndeterminateVisibility(false);
			switch (m.arg1) {
			case (TMDBMovieDownloader.STATUS_IO_ERROR): {
				Toast.makeText(context, getString(R.string.mainIoError),
						Toast.LENGTH_LONG).show();
				break;
			}
			case (TMDBMovieDownloader.STATUS_PARSE_ERROR): {
				Toast.makeText(context, getString(R.string.mainParseError),
						Toast.LENGTH_LONG).show();
				break;
			}
			case (TMDBMovieDownloader.STATUS_OK): {
				break;
			}
			}

		}
	};
	
}